<?php
namespace statistics\view\renderers;

/**
 * Parent-class for all element-renderers. Contains the content elements of this element and the rendered HTML.
 * TODO: rendered HTML should only be stored in corresponding element-renderer-object. 
 *
 * @author AMD-User
 *
 */
abstract class ElementRenderer{
	private static $instCount = 1;
	protected $renderedElementHTML;
	private  $contentElements;
	
	public function __construct(){
		$this->renderedElementHTML = '';
		$this->contentElements = array();
	}
	
	final public function addContentElement(ElementRenderer $contentElement){
		$this->contentElements[] = $contentElement;
	}
	
	abstract public function renderElement();
	
	final protected function renderElementContent(){
		foreach ($this->contentElements as $contentElement){
			$contentElement->renderElement();
			$this->renderedElementHTML .= $contentElement->getRenderedElementHTML();
		}
	}
	
	public function getRenderedElementHTML(){
		if ($this->renderedElementHTML==''){
			$this->renderElement();
		}
// 		echo 'Element '.self::$instCount.' rendered: '.$this->renderedElementHTML;
// 		self::$instCount++;
		return $this->renderedElementHTML;
	}
	
	public function flushRenderedElementHTML(){
		$this->renderedElementHTML = '';
		foreach ($this->contentElements as $contentElement){
			$contentElement->renderElement()->flushRenderedElementHTML();
		}
		
	}
}

?>